#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <signal.h>



pthread_attr_t attr;


unsigned int cnt = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t	cond = PTHREAD_COND_INITIALIZER;

#define NR_MAX	10
#define NR_HALF1	3
#define NR_HALF2	6

void *pthread( void * );

void * pthread1( void *arg ) 
{
	
//	pthread_detach( pthread_self(  ) );
//	sleep( 1 );

	while ( 1 )
		pause();
	
	for ( ;; ) {
		pthread_mutex_lock( &mutex );
		
		pthread_cond_wait( &cond, &mutex );		
		cnt++;
		printf( "conut var in the %s, [%d].\n", __FUNCTION__, cnt );
		
		pthread_mutex_unlock( &mutex );

		if ( cnt >= NR_MAX )
			return NULL;
	}


	pthread_exit((void*) arg);

}

void * pthread2( void *arg ) 
{
	while ( 1 )
		pause();

	for ( ;; ) {
		pthread_mutex_lock( &mutex );
		if ( cnt < NR_HALF1 || cnt > NR_HALF2 ) 
			pthread_cond_signal( &cond );
		else {
			cnt++;	
			printf( "conut var in the %s, [%d].\n", __FUNCTION__, cnt );

		}

		pthread_mutex_unlock( &mutex );

		if ( cnt >= NR_MAX )
			return NULL;
	}

	return NULL;
}

static void pthread_sig( int sig )
{
	printf( "Signal [ %d ].\n", sig );

	return ;
}

int main( void )
{
	sigset_t newmask, oldmask;

	pthread_t tid1, tid2;
	size_t stacksize;

	sigemptyset( &newmask );
	sigemptyset( &oldmask );

	//sigfillset( &newmask );
	sigaddset( &newmask, SIGKILL );

	pthread_sigmask( SIG_BLOCK, &newmask, &oldmask );

	signal( SIGKILL, pthread_sig );
	
	pthread_create( &tid1, &attr, pthread1, NULL );
	pthread_create( &tid2, &attr, pthread2, NULL );
	pthread_join( tid1, NULL );
	pthread_join( tid2, NULL );


	sleep(1);

	printf( "End cnt [ %d ].\n", cnt );
	

	return 0;
}

